The present invention generally relates to the field of database transactions, and particularly to recovery of a write ahead log after an interruption.
The efficient and persistent storage of electronic data is of the utmost importance in most every aspect of modem life. There are a variety of ways, which may be utilized to improve data storage efficiency. For example, a client may start a transaction, a database is updated and committed to disk, and the transaction completed. The client is then free to perform another transaction. The database prevents other clients from performing transactions that would also update shared areas in the database until the first client""s transaction completes. This limits concurrency by forcing transactions to be single threaded.
To improve performance, a database may perform multiple transactions in batches. Multiple transactions that update the database may start, but should not complete until all changes to the database have been made and the database committed to disk. At some point, new transaction may be put on hold and not allowed to start so that an earlier transaction may have a chance to complete. This is an improvement over single threaded transactions, but may still cause transactions to wait for the completion of other transactions.
A write ahead log is used to further improve performance by improving transaction concurrency to a database. A write ahead log is a sequentially written log file that contains the essence of the transaction. For example, as each transaction starts, a database is updated in memory, the essence of the transaction is written to a write ahead log and committed to disk, and the transaction completes. Thus, it does not have to wait for the database to be written to disk. The updated database in memory may be written to disk at any time after the transaction has been committed to the write ahead log. However, if the system, running the database, was interrupted by a reset or power cycle the database may have incomplete transactions. The write ahead log may then be used to rebuild the changes to the database.
One problem is that a write ahead log is typically circular, with a head and tail, and is written sequentially to disk one block at a time. Multiple transactions may be placed in each block, but is typically limited to waiting for an earlier write to complete.
A sequence number is typically maintained in each block so that after an interruption, the head and tail may be re-determined. If the write to the next log block is interrupted, then during recover, the transaction information in the log block could be old information (meaning the write never took place), new information (meaning the write completed successfully), or indeterminate information. If it is old or new, then the head and tail may be determined. If it is indeterminate information due to the interruption, then the information may be ignored, as though it had not been written. If it is indeterminate information due to some sort of data corruption after the log write completed successfully, then the log and the database must be considered invalid and unusable. Thus, a problem may be encountered in distinguishing between the last two cases.
Therefore, it would be desirable to provide a method for recovery of a write ahead log after an interruption.
Accordingly, the present invention is directed to a method of recovering a write ahead log after an interruption. In a first aspect of the present invention, a method of writing a log entry of a write ahead log includes initiating a log write to a write ahead log, the write ahead log having a first sector, and a second sector, wherein the first sector is followed by the second sector. A log entry including a sequence number is written to the second sector. Then, the log entry including the sequence number is written to the first sector.
In a second aspect of the present invention, a method of writing log entries of a write ahead log, includes initiating a log write to a write ahead log, the write ahead log having a first sector, a second sector and a third sector. The first sector is followed by the second sector and the second sector is followed by the third sector, the first sector being an initial head of the write ahead log. A log entry including a sequence number is written to the third sector. Then, the log entry including the sequence number is written to the second sector.
In a third aspect of the present invention, a data storage system, includes a data storage device suitable for storage of electronic data. The data storage device is configured to include a write ahead log, the write ahead log having a first sector, a second sector and a third sector. The first sector is followed by the second sector and the second sector is followed by the third sector, the first sector being an initial head of the write ahead log. A controller suitable for configuring the data storage system to perform a program of instructions is communicatively coupled to the data storage device. When the controller receives a request to perform a write to the write ahead log, the controller configures the data storage device to write a log entry including a sequence number to the third sector and then write the log entry including the sequence number to the second sector.
It is to be understood that both the forgoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the invention as claimed. The accompanying drawings, which are incorporated in and constitute a part of the specification, illustrate an embodiment of the invention and together with the general description, serve to explain the principles of the invention.